home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / shoot / athrust.lha / AmigaThrust / src / amigakey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-05  |  6.7 KB  |  348 lines

  1. /*
  2.  * amigakey.c
  3.  * Amiga specific keyboard routines for Thrust.
  4.  * Written by Frank Wille, frank@phoenix.owl.de
  5.  *
  6.  */
  7.  
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #include <exec/types.h>
  16. #include <exec/libraries.h>
  17. #include <exec/io.h>
  18. #include <devices/inputevent.h>
  19. #include <intuition/intuition.h>
  20. #include <rtgmaster/rtgmaster.h>
  21. #include <rtgmaster/rtgsublibs.h>
  22. #include <proto/exec.h>
  23. #include <proto/rtgmaster.h>
  24. #include <proto/console.h>
  25.  
  26. #include "keyboard.h"
  27. #include "ksyms.h"
  28. #include "amigakey.h"
  29.  
  30. #define NR_KEYS (0x68)
  31.  
  32.  
  33. extern struct RtgScreen *RtgScreen;  /* from amiga.c */
  34.  
  35. struct Library *ConsoleDevice = NULL;
  36. static struct IOStdReq *conio;
  37.  
  38. int scancode[5] = {
  39.   SCANCODE_A,
  40.   SCANCODE_S,
  41.   SCANCODE_RIGHTALT,
  42.   SCANCODE_ENTER,
  43.   SCANCODE_SPACE
  44. };
  45.  
  46. static char keys[NR_KEYS];
  47. static char *keynames[NR_KEYS];
  48. static char *qualifiernames[] = {
  49.   "Shift_L","Shift_R","Caps_Lock","Control",
  50.   "Alt_L","Alt_R","Amiga_L","Amiga_R"
  51. };
  52.  
  53.  
  54.  
  55. static int rawkeyconv(char *buf,int bufsize,unsigned char code)
  56. {
  57.   static struct InputEvent ie = {
  58.     NULL,IECLASS_RAWKEY,0,0,0
  59.   };
  60.  
  61.   if (code & 0x80)
  62.     return (0);
  63.   ie.ie_Code = (UWORD)code;
  64.   ie.ie_Qualifier = 0;
  65.   ie.ie_position.ie_addr = NULL;  /* ??? @@@@ */
  66.   return (RawKeyConvert(&ie,(UBYTE *)buf,(LONG)bufsize,NULL));
  67. }
  68.  
  69.  
  70. static void flushbuffer()
  71. {
  72.   struct IntuiMessage *imsg;
  73.  
  74.   while (imsg = (struct IntuiMessage *)RtgGetMsg(RtgScreen)) {
  75.     if (imsg->Class == IDCMP_RAWKEY)
  76.       keys[imsg->Code & 0x7f] = !(imsg->Code & 0x80);
  77.     RtgReplyMsg(RtgScreen,imsg);
  78.   }
  79. }
  80.  
  81.  
  82. static void clearmatrix()
  83. {
  84.   int i = NR_KEYS+1;
  85.   char *k = keys;
  86.  
  87.   while (--i)
  88.     *k++ = 0;
  89. }
  90.  
  91.  
  92. void singlekey(void)
  93. {
  94.   flushbuffer();
  95. }
  96.  
  97.  
  98. void multiplekeys(void)
  99. {
  100.   clearmatrix();
  101. }
  102.  
  103.  
  104. int getonemultiplekey(void)
  105. {
  106.   struct IntuiMessage *imsg;
  107.   int i, gotkey=999;
  108.  
  109.   for (i=0; i<NR_KEYS; i++) {
  110.     if (keys[i]) {
  111.       gotkey = i;
  112.       break;
  113.     }
  114.   }
  115.   while (gotkey == 999) {
  116.     usleep(50000L);
  117.     while (imsg = (struct IntuiMessage *)RtgGetMsg(RtgScreen)) {
  118.       if (imsg->Class == IDCMP_RAWKEY) {
  119.         i = imsg->Code & 0x7f;
  120.         if (keys[i] = !(imsg->Code & 0x80)) {
  121.           if (i < gotkey)
  122.             gotkey = i;
  123.         }
  124.       }
  125.       RtgReplyMsg(RtgScreen,imsg);
  126.     }
  127.   }
  128.   while (keys[gotkey]) {
  129.     usleep(50000L);
  130.     flushbuffer();
  131.   }
  132.   return (gotkey);
  133. }
  134.  
  135.  
  136. int getkey(void)
  137. {
  138.   struct IntuiMessage *imsg;
  139.   int c = 0;
  140.   char buf[8];
  141.  
  142.   while (imsg = (struct IntuiMessage *)RtgGetMsg(RtgScreen)) {
  143.     if (imsg->Class == IDCMP_RAWKEY) {
  144.       keys[imsg->Code & 0x7f] = !(imsg->Code & 0x80);
  145.       if (!(imsg->Code & 0x80))
  146.         if (rawkeyconv(buf,8,(unsigned char)(imsg->Code & 0x7f)) == 1)
  147.           c = (int)buf[0];
  148.     }
  149.     RtgReplyMsg(RtgScreen,imsg);
  150.     if (c) break;
  151.   }
  152.   return (c);
  153. }
  154.  
  155.  
  156. unsigned char getkeys(void)
  157. {
  158.   unsigned char keybits=0;
  159.  
  160.   flushbuffer();
  161.   if (keys[SCANCODE_P])
  162.     keybits |= pause_bit;
  163.   else if (keys[SCANCODE_ESCAPE] || keys[SCANCODE_Q])
  164.     keybits |= escape_bit;
  165.   if (keys[scancode[0]])
  166.     keybits |= left_bit;
  167.   if (keys[scancode[1]])
  168.     keybits |= right_bit;
  169.   if (keys[scancode[2]])
  170.     keybits |= thrust_bit;
  171.   if (keys[scancode[3]])
  172.     keybits |= fire_bit;
  173.   if (keys[scancode[4]])
  174.     keybits |= pickup_bit;
  175.  
  176.   return keybits;
  177. }
  178.  
  179.  
  180. char *keystring(int key)
  181. {
  182.   static char keybuffer[100];
  183.   int i;
  184.  
  185.   if(key<0 || key>=NR_KEYS)
  186.     return NULL;
  187.  
  188.   strncpy(keybuffer, keynames[key], 99);
  189.   keybuffer[99] = '\0';
  190.   for(i=0; i<strlen(keybuffer); i++)
  191.     if(keybuffer[i] == '_')
  192.       keybuffer[i] = ' ';
  193.  
  194.   return keybuffer;
  195. }
  196.  
  197.  
  198. int keycode(char *keyname)
  199. {
  200.   static char keybuffer[100];
  201.   int i;
  202.  
  203.   strncpy(keybuffer, keyname, 99);
  204.   keybuffer[99] = '\0';
  205.   for(i=0; i<strlen(keybuffer); i++)
  206.     if(keybuffer[i] == ' ')
  207.       keybuffer[i] = '_';
  208.  
  209.   for(i=0; i<NR_KEYS; i++) {
  210.     if(!strcasecmp(keynames[i], keybuffer))
  211.       return(i);
  212.   }
  213.  
  214.   return(0);
  215. }
  216.  
  217.  
  218. void flushkeyboard(void)
  219. {
  220.   flushbuffer();
  221.   clearmatrix();
  222. }
  223.  
  224.  
  225. int keywaiting(void)
  226. {
  227.   int i = NR_KEYS+1;
  228.   char *k = keys;
  229.  
  230.   while (--i) {
  231.     if (*k++)
  232.       return (1);
  233.   }
  234.   return (0);
  235. }
  236.  
  237.  
  238. int keyinit(void)
  239. {
  240.   struct MsgPort *conport;
  241.   char buf[8];
  242.   int i,n;
  243.  
  244.   if (RtgInitRDCMP(RtgScreen)) {
  245.  
  246.     /* open console.device */
  247.     if (conport = CreateMsgPort()) {
  248.       if (conio = CreateIORequest(conport,sizeof(struct IOStdReq))) {
  249.         if (OpenDevice("console.device",-1,
  250.                        (struct IORequest *)conio,0) == 0) {
  251.           ConsoleDevice = (struct Library *)conio->io_Device;
  252.         }
  253.         else {
  254.           DeleteIORequest(conio);
  255.           DeleteMsgPort(conport);
  256.         }
  257.       }
  258.       else
  259.         DeleteMsgPort(conport);
  260.     }
  261.  
  262.     /* determine key names */
  263.     if (ConsoleDevice) {
  264.       static char *unknown = "unknown";
  265.  
  266.       for (i=0; i<NR_KEYS; i++) {
  267.         keys[i] = 0;
  268.         keynames[i] = unknown;
  269.         if (i >= 0x60) {
  270.           keynames[i] = qualifiernames[i-0x60];
  271.         }
  272.         else if (i>=0x50 && i<=0x59) {
  273.           keynames[i] = syms[1].table[i-0x50];
  274.         }
  275.         else if ((n = rawkeyconv(buf,8,(unsigned char)i)) > 0) {
  276.           if (n == 1) {
  277.             switch ((unsigned char)buf[0]) {
  278.               case 13:
  279.                 if (i & 1)
  280.                   keynames[i] = "KP_Enter";
  281.                 else
  282.                   keynames[i] = "Return";
  283.                 break;
  284.               default:
  285. #if 0
  286.                 if (i==0x0f || (i>=0x1d&&i<0x20) || (i>=0x2d&&i<0x30)
  287.                     || (i>=0x3c&&i<0x40) || i==0x4a || (i>=0x5a && i<0x60)) {
  288.                   keynames[i] = /* keypad symbol... */
  289. #endif
  290.                 keynames[i] = syms[0].table[(unsigned char)buf[0]];
  291.                 if (*keynames[i] == 0)
  292.                   keynames[i] = unknown;
  293.                 break;
  294.             }
  295.           }
  296.           else {
  297.             if ((unsigned char)buf[0] == 0x9b) {
  298.               switch (buf[1]) {
  299.                 case 0x41:
  300.                   keynames[i] = "Cursor_Up";
  301.                   break;
  302.                 case 0x42:
  303.                   keynames[i] = "Cursor_Down";
  304.                   break;
  305.                 case 0x43:
  306.                   keynames[i] = "Cursor_Right";
  307.                   break;
  308.                 case 0x44:
  309.                   keynames[i] = "Cursor_Left";
  310.                   break;
  311.               }
  312.             }
  313.           }
  314.         }
  315.       }
  316.     }
  317.     else {
  318.       printf("Couldn't open console.device.\n");
  319.       return (-1);
  320.     }
  321.   }
  322.   else {
  323.     printf("RDCMP init failed!\n");
  324.     return (-1);
  325.   }
  326.   return (0);
  327. }
  328.  
  329.  
  330. int keyclose(void)
  331. {
  332.   if (ConsoleDevice) {
  333.     CloseDevice((struct IORequest *)conio);
  334.     DeleteMsgPort(conio->io_Message.mn_ReplyPort);
  335.     DeleteIORequest(conio);
  336.     ConsoleDevice = NULL;
  337.   }
  338.   return (0);
  339. }
  340.  
  341.  
  342. char *keyname(void)
  343. {
  344.   static char name[] = "RDCMP";
  345.  
  346.   return name;
  347. }
  348.